const app = getApp()
var webhost = app.globalData.webhost;
var getInstance;
var insClick;

Page({
  data: {
    dataList: [1,2,3],
    plan: {},
    show: false,
    actions: [],
    index: 0,
  },

  call(e){
    var phone = e.currentTarget.dataset.phone;
    if(phone){
      wx.makePhoneCall({
        phoneNumber: phone
      })
    }
  },

  onClose() {
    this.setData({ show: false });
  },

  onSelect(event) {
    this.setData({ show: false });
    switch (event.detail.name) {
      case "异常登记":
        wx.navigateTo({
          url: '../func/doException?task=1',
        })
        break;
      case "费用申请":
        wx.navigateTo({
          url: '../func/feeApplication?task=1',
        })
        break;
      case "照片":
        wx.navigateTo({
          url: './img?lt=task',
        }) 
        break;
    }
  },

  setClick(e) {
    var that = this;
    var taskid = e.currentTarget.dataset.id;
    var taskCode = e.currentTarget.dataset.taskcode
    var actionCode = e.currentTarget.dataset.actioncode;
    // console.log(taskCode);
    // console.log(actionCode);
    // return false;
    insClick(app.globalData.planid, taskCode, taskid, actionCode);
  },

  moreClick(e) {
    var that = this;
    var id = +e.currentTarget.dataset.id;
    that.setData({ index: id })
    var task = that.data.dataList[id];
    app.globalData.task = task;
    var list = new Array();
    if (task.abnormal) {
      var a = '异常登记';
      // var a = { name: '异常登记' };
      list.push(a);
    }
    if (task.cost) {
      // var b = { name: "费用申请" };
      var b = "费用申请";
      list.push(b);
    }
    if (task.snap) {
      var b = "照片";
      // var b = { name: "照片" };
      list.push(b);
    }
    that.setData({ actions: list });
    // that.setData({ show: true });
    wx.showActionSheet({
      itemList: that.data.actions,
      success(res) {
        var item = that.data.actions[res.tapIndex];
        switch (item) {
          case "异常登记":
            wx.navigateTo({
              url: '../func/doException?task=1',
            })
            break;
          case "费用申请":
            wx.navigateTo({
              url: '../func/feeApplication?task=1',
            })
            break;
          case "照片":
            wx.navigateTo({
              url: './img?lt=task',
            })
            break;
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  viewClick(e) {
    var id = +e.currentTarget.dataset.id;
    var task = this.data.dataList[id];
    app.globalData.taskid = task.taskid;
    app.globalData.task = task;
    wx.navigateTo({
      url: './mdetail'
    })
  }, 

  onLoad: function (options) {
    var that = this;

    that.setData({
      plan: app.globalData.plan
    })

    getInstance = function(){
      wx.showNavigationBarLoading();
      var data = [{
        String: {
          driverAccount: app.globalData.driverAccount,
          sessionid: app.globalData.sessionid,
          planId: app.globalData.planid
        }
      }]
      wx.request({
        url: webhost,
        data: {
          eap_username: "driver",
          eap_password: "Driver2018",
          boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
          methodName: 'queryPlanTaskList', //调用的具体方法
          returnType: "json", //返回参数类型
          parameters: JSON.stringify(data)
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        complete() {
          wx.hideNavigationBarLoading();
        },
        success({ data }) {
          if (data.code && data.code == '000000') {
            var list = data.dataList;            
            var New = new Array();
            for (var i = 0; i < list.length; i++) {
              var item = list[i];
              if (item.actionList){
                for (var j = 0; j < item.actionList.length; j++) {
                  switch (item.actionList[j].actionCode) {
                    case 'AC10':
                      item.start = item.actionList[j];
                      break;
                    case 'AC20':
                      item.build = item.actionList[j];
                      break;
                    case 'AC30':
                      item.end = item.actionList[j];
                      break;
                    case 'AC40':
                      item.stop = item.actionList[j];
                      break;
                    case 'AC50':
                      item.return = item.actionList[j];
                      break;
                    case 'EXCE':
                      item.abnormal = item.actionList[j];
                      break;
                    case 'COST':
                      item.cost = item.actionList[j];
                      break;
                    case 'SNAP':
                      item.snap = item.actionList[j];
                      break;
                  }
                }
              }
              New.push(item);
            }
            console.log(New);
            that.setData({
              dataList: New
            })
          } else if (data.code == '003000') {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
            setTimeout(function () {
              wx.redirectTo({
                url: '../login/login',
              })
            }, 1200);
          } else {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }
        },
        fail: function () {
          wx.showToast({
            title: '网络连接失败',
            icon: 'none'
          })
        }
      })
    }

    insClick = function (planid, taskCode, taskid, actionCode) {
      wx.showNavigationBarLoading();
      var data = [{
        String: {
          driverAccount: app.globalData.driverAccount,
          sessionid: app.globalData.sessionid,
          planId: planid,
          taskid: taskid,
          taskCode: taskCode,
          actionCode: actionCode,
          remarks: ''
        }
      }]
      console.log(data);
      wx.request({
        url: webhost,
        data: {
          eap_username: "driver",
          eap_password: "Driver2018",
          boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
          methodName: 'doGeneralEvent', //调用的具体方法
          returnType: "json", //返回参数类型
          parameters: JSON.stringify(data)
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        complete() {
          wx.hideNavigationBarLoading();
        },
        success({ data }) {
          if (data.code && data.code == '000000') {
            wx.showToast({
              title: '操作成功',
              icon: 'success'
            })
            getInstance();
          } else {
            wx.showModal({
              title: '操作异常',
              showCancel: false,
              confirmColor: '#118EDE',
              content: data.msg,
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '网络连接失败',
            icon: 'none'
          })
        }
      })
    }

  },

  onShow: function () {
    var that = this;

    getInstance();    
  },

  onPullDownRefresh: function () {

  },

  onShareAppMessage: function () {

  }
})